package team.first.mongodb;

import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import cn.hutool.db.handler.BeanListHandler;
import com.mongodb.MongoClientSettings;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import dev.morphia.Datastore;
import dev.morphia.Morphia;
import team.first.mongodb.doc.*;

import java.sql.SQLException;
import java.util.Arrays;
import java.util.List;

/**
 * @Author: HAPPY
 * @Project_name: project
 * @Package_name: team.first.mongodb.entity
 * @Date: 2021/11/18 20:36
 * @Description：
 */
public class MorphiaMovieImport {
    public static final String MONGO_DB_NAME = "movie_db";
    public static final String MONGO_DB_USERNAME = "user";
    public static final String MONGO_DB_PASSWORD = "123456";

    public static void main(String[] args) {
        // Mongo数据库地址
        ServerAddress serverAddress = new ServerAddress("localhost", 27017);

        // Mongo数据库的认证信息
        MongoCredential mongoCredential = MongoCredential.createCredential(MONGO_DB_USERNAME, MONGO_DB_NAME,
                MONGO_DB_PASSWORD.toCharArray());
        MongoClientSettings mongoClientSettings = MongoClientSettings.builder()
                .applyToClusterSettings(builder ->
                        builder.hosts(Arrays.asList(serverAddress)))
                .credential(mongoCredential).build();

        // MongoClient必须是com.mongodb.client.MongoClient
        MongoClient mongoClient = MongoClients.create(mongoClientSettings);
        Datastore datastore = Morphia.createDatastore(mongoClient, MONGO_DB_NAME);
        datastore.getMapper().mapPackage("team.first.mongodb.doc");

        // 将数据库中结果插入
        String sqlGetAllMovies = "select * from movie";
        String sqlGetAllActors = "select staff.*, movie_cast.role_type_id, movie_cast.act_as from staff,movie_cast where movie_id = ? and staff_id = staff.id and role_type_id = 1";
        String sqlGetAllDirectors = "select staff.*, movie_cast.role_type_id, movie_cast.act_as from staff,movie_cast where movie_id = ? and staff_id = staff.id and role_type_id = 2";
        String sqlGetAllGenres = "select id, name from genre where id in (" +
                "select genre_id from movie_genre where movie_id = ?)";

        try {
            // 查出所有员工
            List<Entity> movieList = Db.use().query(sqlGetAllMovies);

            movieList.forEach(movie -> {
                try {
                    // 转换bean
                    MovieDoc movieDic = movie.toBean(MovieDoc.class);

                    List<InnerStaffDoc> actors = Db.use()
                            .query(sqlGetAllActors, new BeanListHandler<>(InnerStaffDoc.class), movie.getInt("id"));
                    List<InnerStaffDoc> directors = Db.use()
                            .query(sqlGetAllDirectors, new BeanListHandler<>(InnerStaffDoc.class), movie.getInt("id"));
                    List<GenreDoc> genreDocList = Db.use()
                            .query(sqlGetAllGenres, new BeanListHandler<>(GenreDoc.class), movie.getInt("id"));

                    movieDic.setActors(actors).setDirectors(directors).setGenres(genreDocList);
                    datastore.ensureIndexes();// 创建索引的

                    datastore.save(movieDic);
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            });
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }
}
